//
//  MCViewController-PageBarAddition.m
//  MClient
//
//  Created by  史正烨 on 10-9-16.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "MCErrorManagement.h"
#import "MCViewController(KeyboardEvent).h"
#import "ListCell.h"

#import "AListController.h"
#import "AListLookupController.h"
#import "CustomAListController.h"
#import "CustomListController.h"
#import "ListController.h"
#import "LookupController.h"

@implementation MCViewController(KeyboardEvent)

// Called when the UIKeyboardWillShowNotification is sent.
- (void)keyboardWillShown:(NSNotification*)aNotification
{
	@try {
		UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
		UIView* keyboard;
		for(int i=0; i<[tempWindow.subviews count]; i++) {
			keyboard = [tempWindow.subviews objectAtIndex:i];
			if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
				for (UIView* subView in keyboard.subviews) {
					if ([subView isKindOfClass:[UIButton class]]) {
						[subView removeFromSuperview];
						break;
					}
				}
			}
		}
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"100" returnMessage:[e reason]];
	}
}

// Called when the UIKeyboardWillShowNotification is sent.
- (void)keyboardDidShown:(NSNotification*)aNotification
{
	@try {
		NSDictionary* info = [aNotification userInfo];
		
		// Get the size of the keyboard.
		NSValue* aValue = [info objectForKey:@"UIKeyboardBoundsUserInfoKey"];
		CGSize keyboardSize = [aValue CGRectValue].size;
		
		if ([self isKindOfClass:[AListController class]] ||
			[self isKindOfClass:[AListLookupController class]] ||
			[self isKindOfClass:[CustomAListController class]] || 
			[self isKindOfClass:[CustomListController class]] ||
			[self isKindOfClass:[ListController class]] ||
			[self isKindOfClass:[LookupController class]]) {

			float nTabBar = CGRectGetHeight(self.tabBarController.tabBar.frame);
			//工具条放在下面的代码
			for (UIView* subView in self.clientView.subviews) {
				if ([subView isKindOfClass:[UITableView class]] ||
					[subView isKindOfClass:[ListCell class]] ||
					[subView isKindOfClass:[CustomListCell class]] ||
					[subView isKindOfClass:[LookupCell class]]) {
					subView.frame = CGRectMake(0, 0, self.width, self.height-keyboardSize.height-nToolBarHeight+nTabBar);
				}
				if ([subView isKindOfClass:[UIToolbar class]]) {
					subView.frame = CGRectMake(0, self.height-keyboardSize.height-nToolBarHeight+nTabBar, self.width, nToolBarHeight);
				}
			}
		}
		
		UITextField* textField = (UITextField*)[self findFirstResponder];
		if (textField.keyboardType == UIKeyboardTypeNumberPad) {
			UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
			UIView* keyboard;
			for(int i=0; i<[tempWindow.subviews count]; i++) {
				keyboard = [tempWindow.subviews objectAtIndex:i];
				if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES ||
				   [[keyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
					UIButton* doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
					if (CGRectGetHeight(self.view.bounds) > CGRectGetWidth(self.view.bounds)) {//竖屏
						doneButton.frame = CGRectMake(0, 163, 106, 53);
					} else {
						doneButton.frame = CGRectMake(0, 120, 158, 41);
					}
					doneButton.adjustsImageWhenHighlighted = NO;
					[doneButton setTitle:@"Done" forState:UIControlStateNormal];
					[doneButton setTitle:@"Done" forState:UIControlStateHighlighted];
					[doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
					[doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
					//[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
					//[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
					[doneButton addTarget:self action:@selector(doneButton) forControlEvents:UIControlEventTouchUpInside];
					
					[keyboard addSubview:doneButton];
				}
			}
		}
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"100" returnMessage:[e reason]];
	}
}


// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardDidHidden:(NSNotification*)aNotification
{
	@try {
		if ([self isKindOfClass:[AListController class]] ||
			[self isKindOfClass:[AListLookupController class]] ||
			[self isKindOfClass:[CustomAListController class]] || 
			[self isKindOfClass:[CustomListController class]] ||
			[self isKindOfClass:[ListController class]] ||
			[self isKindOfClass:[LookupController class]]) {
			
			//工具条放在下面的代码
			for (UIView* subView in self.clientView.subviews) {
				if ([subView isKindOfClass:[UITableView class]] ||
					[subView isKindOfClass:[ListCell class]] ||
					[subView isKindOfClass:[CustomListCell class]] ||
					[subView isKindOfClass:[LookupCell class]]) {
					subView.frame = CGRectMake(0, 0, self.width, self.height-nToolBarHeight);
				}
				if ([subView isKindOfClass:[UIToolbar class]]) {
					subView.frame = CGRectMake(0, self.height-nToolBarHeight, self.width, nToolBarHeight);
				}
			}
		}
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"100" returnMessage:[e reason]];
	}
}

- (void)doneButton
{
	UIView* view = [self findFirstResponder];
	if ([view isKindOfClass:[UITextField class]] ||
		[view isKindOfClass:[UITextView class]]) {
		[[self findFirstResponder] resignFirstResponder];
	}
}

@end
